package com.qzone;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class StudentList extends Activity {

	ListView stdlist;
	DBHelper dbHelper;
	Context mContext;
	private static long back_pressed;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext=this;
		
		setContentView(R.layout.activity_student_list);
		
		stdlist=(ListView)findViewById(R.id.stdlist);
		dbHelper=new DBHelper(mContext);

		Cursor cursor=dbHelper.retriveAllRecord();
		
	//cursor.moveToFirst();
	   final ArrayList<String> schoolname=new ArrayList<String>();
	   final ArrayList<String> regnumber=new ArrayList<String>();
	   final ArrayList<String> studentname=new ArrayList<String>();
	   final ArrayList<String> tagno=new ArrayList<String>();
	   final ArrayList<String> masterid=new ArrayList<String>();
	   final ArrayList<String> sipnumber=new ArrayList<String>();
	   while(cursor.moveToNext()) 
	   {
		   schoolname.add(cursor.getString(cursor.getColumnIndex("SchoolName")));
		   regnumber.add(cursor.getString(cursor.getColumnIndex("ROLLNo")));
		   studentname.add(cursor.getString(cursor.getColumnIndex("StudentName")));
		   tagno.add(cursor.getString(cursor.getColumnIndex("TagNo")));
		   masterid.add(cursor.getString(cursor.getColumnIndex("MasterId")));
		   sipnumber.add(cursor.getString(cursor.getColumnIndex("SIPNumber")));
       }

	   CustomAdapter customAdapter=new CustomAdapter(this, schoolname,regnumber,studentname);
	   stdlist.setAdapter(customAdapter);
	   
	   stdlist.setOnItemClickListener(new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			
			Intent intent=new Intent(mContext,LocationList.class);
			intent.putExtra("SIPNUMBER", sipnumber.get(arg2).toString());
			intent.putExtra("STUDENTNAME", studentname.get(arg2).toString());
			intent.putExtra("REGNUMBER", regnumber.get(arg2).toString());
			intent.putExtra("SCHOOLNAME", schoolname.get(arg2).toString());
			intent.putExtra("MASTERID", masterid.get(arg2).toString());
			intent.putExtra("TAGNO", tagno.get(arg2).toString());
			startActivity(intent);
			finish();
		}
		   
	});
	   
	   stdlist.setOnItemLongClickListener(new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			// TODO Auto-generated method stub
			String MasterId = masterid.get(arg2).toString();
			CommonUtil.deleteStudent(mContext, MasterId);
			return true;
		}
	});  
		
	}

	@Override
	public void onBackPressed()
	{
	        if (back_pressed + 2000 > System.currentTimeMillis()) super.onBackPressed();
	        else Toast.makeText(getBaseContext(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
	        back_pressed = System.currentTimeMillis();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.stuadd, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.addstudent) {
			Intent intent=new Intent(mContext,AddStudent.class);
			startActivity(intent);
			finish();
			return true;
		}

		return super.onOptionsItemSelected(item);
	}
	
	static class CustomAdapter extends BaseAdapter{

		private Context mC;
		private ArrayList schoolname,regnumber,studentname;
		private static LayoutInflater inflater=null;
		public CustomAdapter(Context c,ArrayList<String> al1,ArrayList<String> al2,ArrayList<String> al3) {
			// TODO Auto-generated constructor stub
			mC=c;
			schoolname=al1;
			regnumber=al2;
			studentname=al3;
			inflater = ( LayoutInflater )mC.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return schoolname.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			View vi = arg1;
			TextView regno,school,stdname;
			if(arg1==null)
			{
				vi = inflater.inflate(R.layout.stdlist, null);
				//vi = inflater.inflate(R.layout.loclist, null);
				
			}
			regno=(TextView)vi.findViewById(R.id.regno);
			school=(TextView)vi.findViewById(R.id.school);
			stdname=(TextView)vi.findViewById(R.id.stdname);
			
			school.setText(schoolname.get(arg0).toString());
			regno.setText(regnumber.get(arg0).toString());
			stdname.setText(studentname.get(arg0).toString());
			
			return vi;
		}
		
	}
}
